package mobile.game.core.support.function;

/**
 * @author gaopan
 *
 * 回掉函数包装
 */
public class CommonFunction {
	/** 回掉函数参数数量 */
	private int paramCount = 0;
	/** 回掉函数 */
	private Object function;
	
	public CommonFunction(Object function, int paramCount){
		this.paramCount = paramCount;
		this.function = function;
	}
	
	public int getParamCount(){
		return this.paramCount;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void apply(Object ... m) {
		assert(m.length == this.paramCount);
		Object f = this.function;
		switch (m.length) {
			case 0: ((JowFunction0) f).apply(); break;
			case 1: ((JowFunction1) f).apply(m[0]); break;
			case 2: ((JowFunction2) f).apply(m[0], m[1]); break;
			case 3: ((JowFunction3) f).apply(m[0], m[1], m[2]); break;
			case 4: ((JowFunction4) f).apply(m[0], m[1], m[2], m[3]); break;
			case 5: ((JowFunction5) f).apply(m[0], m[1], m[2], m[3], m[4]); break;
			case 6: ((JowFunction6) f).apply(m[0], m[1], m[2], m[3], m[4], m[5]); break;
			case 7: ((JowFunction7) f).apply(m[0], m[1], m[2], m[3], m[4], m[5], m[6]); break;
			case 8: ((JowFunction8) f).apply(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7]); break;
			case 9: ((JowFunction9) f).apply(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]); break;
			default: break;
		}
	}
	
	/**
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getFunc() {
		return (T) function;
	}
}
